package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.freemark.utils.FreemarkUtil;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.constant.CodeConstant;
import com.taxshare.seller.constant.SellerRedInvoiceConstant;
import com.taxshare.seller.constant.SellerRedInvoiceDetailConstant;
import com.taxshare.seller.entity.*;
import com.taxshare.seller.mapper.SellerRedInvoiceMapper;
import com.taxshare.seller.remote.ApiClientService;
import com.taxshare.seller.util.HttpUtil;
import com.taxshare.seller.util.InvoiceHelper;
import com.taxshare.seller.util.Xml2Map;
import com.taxshare.seller.util.sst.IntfXmlUtil;
import com.taxshare.seller.view.dto.InterfaceDTO;
import com.taxshare.seller.view.vo.*;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.collections.MapUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
@Slf4j
public class RedTicketService extends ServiceImpl<SellerRedInvoiceMapper, SellerRedInvoiceEntity> {

    @Autowired
    SellerRedInvoiceDetailService sellerRedInvoiceDetailService;

    @Autowired
    InvoicedService invoicedService;

    @Autowired
    InvoiceDetailService invoiceDetailService;

    @Autowired
    ApiClientService apiClientService;

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    CommService commService;


    String ssturl="https://taxsapi.holytax.com/v1/api/s";

    @Value("${zzsfp.url}")
    private String zzsfpUrl;

    public Response queryTickets(PageRequest<RedTicketReqVO> query) {
        QueryWrapper<SellerRedInvoiceEntity> queryWrapper = new QueryWrapper<>();
        String userID = UserInfoUtil.getUserID();
        RedTicketReqVO data = query.getInfo();
        if (StrUtil.isNotBlank(data.getPurchaserName())) {
            queryWrapper.like(SellerRedInvoiceConstant.PURCHASER_NAME, data.getPurchaserName());
        }
        if (StrUtil.isNotBlank(data.getPurchaserTaxNo())) {
            queryWrapper.eq(SellerRedInvoiceConstant.PURCHASER_TAX_NO, data.getPurchaserTaxNo());
        }
        if (StrUtil.isNotBlank(data.getTicketState())) {
            queryWrapper.eq(SellerRedInvoiceConstant.TICKET_STATE, data.getTicketState());
        }
        if (StrUtil.isNotBlank(data.getInvoiceCode())) {
            queryWrapper.eq(SellerRedInvoiceConstant.INVOICE_CODE, data.getInvoiceCode());
        }
        if (StrUtil.isNotBlank(data.getInvoiceNo())) {
            queryWrapper.eq(SellerRedInvoiceConstant.INVOICE_NO, data.getInvoiceNo());
        }
        if (StrUtil.isNotBlank(data.getRedInfoNo())) {
            queryWrapper.eq(SellerRedInvoiceConstant.RED_INFO_NO, data.getRedInfoNo());
        }
        if (StrUtil.isNotBlank(data.getInputDateStart()) && StrUtil.isNotBlank(data.getInputDateEnd())) {
            queryWrapper.between(SellerRedInvoiceConstant.INPUT_DATE, data.getInputDateStart(), data.getInputDateEnd());
        }
        if (StrUtil.isNotBlank(data.getSellerTaxNo())) {
            queryWrapper.eq(SellerRedInvoiceConstant.SELLER_TAX_NO, data.getSellerTaxNo());
        }
        if (StrUtil.isBlank(data.getSellerTaxNo())) {
            List<String> taxNos = commService.getTaxNumbers();
            queryWrapper.in("seller_tax_no",taxNos);
        } else {
            queryWrapper.eq("seller_tax_no",data.getSellerTaxNo());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<SellerRedInvoiceEntity> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), SellerRedInvoiceEntity.class);
        return Response.success(result);
    }

    /**
     * 申请红字信息表
     *
     * @param reqVo
     * @return
     * @throws Exception
     */
    public Response applyTicket(TicketReqVO reqVo) throws Exception {
        Response response = new Response();
        SellerRedInvoiceEntity redInvoiceEntity = this.getOne(new QueryWrapper<SellerRedInvoiceEntity>().eq("uuid", reqVo.getUuid()));
        if (redInvoiceEntity != null) {
            //根据税盘不同处理
            EquipmentEntity equipmentEntity = equipmentService.getOne(new QueryWrapper<EquipmentEntity>().eq("seller_tax_no", redInvoiceEntity.getSellerTaxNo()));
            if (equipmentEntity == null || CodeConstant.COMM_CODE_0.equals(equipmentEntity.getDisable())) {
                return Response.error(CodeConstant.ERROR, "SEL_038");
            }
            if (CodeConstant.A9.equals(equipmentEntity.getEquipmentMode())) {
                response = applyTicketA9(reqVo);
            } else {
                response = applyTicketSST(equipmentEntity,redInvoiceEntity);
            }
        }
        return response;
    }

    /**
     * 申请红字信息表SST
     *
     * @param
     * @return
     * @throws Exception
     */
    public Response applyTicketSST(EquipmentEntity equipmentEntity, SellerRedInvoiceEntity sellerRedInvoiceEntity) throws Exception {
        List<SellerRedInvoiceDetailEntity> redDetailList = sellerRedInvoiceDetailService.list(new QueryWrapper<SellerRedInvoiceDetailEntity>().eq("red_info_id", sellerRedInvoiceEntity.getUuid()));
        SellerInvoiceEntity one = invoicedService.getOne(new QueryWrapper<SellerInvoiceEntity>().eq("invoice_code", sellerRedInvoiceEntity.getInvoiceCode()).eq("invoice_no", sellerRedInvoiceEntity.getInvoiceNo()));
        if (one != null) {
            String reqXml = IntfXmlUtil.uploadRedTicketInfo(equipmentEntity, sellerRedInvoiceEntity, redDetailList);
            log.info(">申请红字申请单-请求报文：" + reqXml);
            Map<String, String> param = new HashMap<>();
            param.put("bw", reqXml);
            boolean errorFlag = false;
            try {
                Map<String, Object> post = HttpUtil.post(ssturl, param);
                if ("200".equals(MapUtils.getString(post, CodeConstant.STATUSCODE))) {
                    String xml = MapUtils.getString(post, CodeConstant.RESPONSE);
                    log.info(">申请红字申请表-响应报文：" + xml);
                    Map<String, Object> resMap = Xml2Map.Xml2Mapz(xml);
                    Map body = MapUtils.getMap(resMap, "body");
                    if (body != null) {
                        String xxbbh = MapUtils.getString(body, "xxbbh");
                        if (StringUtils.isNotBlank(xxbbh)) {
                            String sqsj = MapUtils.getString(body, "sqsj");
                            if (xxbbh.equals("")) {
                                sellerRedInvoiceEntity.setTicketState("1");
                            } else {
                                sellerRedInvoiceEntity.setTicketState("0");
                            }
                            sellerRedInvoiceEntity.setReqDate(sqsj);
                            sellerRedInvoiceEntity.setInputDate(new SimpleDateFormat("yyyy-MM-dd ").format(new Date()));
                            sellerRedInvoiceEntity.setRedInfoNo(xxbbh);
                            this.updateById(sellerRedInvoiceEntity);
                        } else {
                            errorFlag = true;
                        }
                    }
                } else {
                    errorFlag = true;
                }
            } catch (Exception e) {
                log.info(e.getMessage());
            }
            if (errorFlag) {
                sellerRedInvoiceEntity.setTicketState("2");
                this.updateById(sellerRedInvoiceEntity);
                return Response.error(CodeConstant.ERROR, "SEL_042");
            }
        }
        return Response.success();
    }
    /**
     * 申请红字信息表A9
     * @param reqVo
     * @return
     * @throws Exception
     */
    public Response applyTicketA9(TicketReqVO reqVo) throws Exception {
        boolean errorFlag = false;
        String uuid = reqVo.getUuid();
        SellerRedInvoiceEntity sellerRedInvoiceEntity = this.getById(uuid);
        QueryWrapper<SellerRedInvoiceDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SellerRedInvoiceDetailConstant.RED_INFO_ID, sellerRedInvoiceEntity.getUuid());
        List<SellerRedInvoiceDetailEntity> details = sellerRedInvoiceDetailService.list(queryWrapper);
        sellerRedInvoiceEntity.setDetails(details);
        String redInvoiceData = FreemarkUtil.getTemplateObj("uploadRedInvoice1.ftl", sellerRedInvoiceEntity);
        String url = zzsfpUrl + "busiInterface";
        String interfaceCode = "EI.HZFPSQBSC.U.EC.INTRA";
        String xmlData = InvoiceHelper.getXmlData(redInvoiceData, interfaceCode, sellerRedInvoiceEntity.getSellerTaxNo(), null);
        InterfaceDTO interfaceDTO = new InterfaceDTO();
        interfaceDTO.setXmlData(xmlData);
        interfaceDTO.setUrl(url);
        Response response = apiClientService.callSales(interfaceDTO);
        if (CodeConstant.T0000.equals(response.getCode())) {
            String reqcontent = response.getMsg();
            Map resMap = Xml2Map.Xml2Mapz(reqcontent);
            Map RESPONSE_COMMON_HZFPSQBSC = (Map) resMap.get("RESPONSE_COMMON_HZFPSQBSC");
            Map REDINVREQBILL = (Map) RESPONSE_COMMON_HZFPSQBSC.get("REDINVREQBILL");
            Map GOODSMX = (Map) REDINVREQBILL.get("GOODSMX");
            String xxbbh = GOODSMX.get("RESBILLNO").toString();
            if (xxbbh != null && !"".equals(xxbbh)) {
                sellerRedInvoiceEntity.setRedInfoNo(xxbbh);
                sellerRedInvoiceEntity.setTicketState("0");
                sellerRedInvoiceEntity.setUpdateTime(new Date());
                this.updateById(sellerRedInvoiceEntity);
            } else {
                errorFlag = true;
            }
        }
        if (errorFlag) {
            sellerRedInvoiceEntity.setTicketState("2");
            sellerRedInvoiceEntity.setUpdateTime(new Date());
            this.updateById(sellerRedInvoiceEntity);
            return Response.error("500", "申请失败");
        } else {
            return Response.success();
        }
    }
    /**
     * 同步红字信息表
     *
     * @param data
     * @return
     */
    public Response syncTickets(SyncRedTicketReqVO data) throws Exception {
        Response response = new Response();
        //根据税盘不同处理
        EquipmentEntity equipmentEntity = equipmentService.getOne(new QueryWrapper<EquipmentEntity>().eq("seller_tax_no", data.getSellerTaxNo()));
        if (equipmentEntity == null || CodeConstant.COMM_CODE_0.equals(equipmentEntity.getDisable())) {
            return Response.error("SEL_038");
        }
        if (CodeConstant.COMM_CODE_4.equals(equipmentEntity.getEquipmentMode())) {
            response = syncTicketsA9(data);
        } else {

        }
        return response;
    }

    /**
     * 同步红字信息表A9
     * @param data
     * @return
     * @throws Exception
     */
    public Response syncTicketsA9(SyncRedTicketReqVO data) throws Exception {
        String url = "http://112.94.226.220:8090/zzs_kpfw/busiInterface";
        String interfaceCode = "EI.HZFPSQBSHXZ.U.EC.INTRA";
        String fplx = InvoiceHelper.transFplxdm(data.getInvoiceType());
        data.setInvoiceType(fplx);
        String makeInvoiceData = FreemarkUtil.getTemplateObj("downRedInvoice.ftl", data);
        String xmlData = InvoiceHelper.getXmlData(makeInvoiceData, interfaceCode, data.getSellerTaxNo(), null);
        InterfaceDTO interfaceDTO = new InterfaceDTO();
        interfaceDTO.setXmlData(xmlData);
        interfaceDTO.setUrl(url);
        Response<Object> res = new Response<>();
        try {
            Response response = apiClientService.callSales(interfaceDTO);
            if (response.getCode().equals("0000")) {
                String reqcontent = response.getMsg();
                Map resMap = Xml2Map.Xml2Mapz(reqcontent);
                List<SellerRedInvoiceEntity> tickets = parseSyncTicketsResData(resMap);
                for (SellerRedInvoiceEntity entity1 : tickets) {
                    String redInfoNo = entity1.getRedInfoNo();
                    // 根据信息表编号查询是否已存在此红字信息表
                    SellerRedInvoiceEntity red = this.getOne(new QueryWrapper<SellerRedInvoiceEntity>().eq(SellerRedInvoiceConstant.RED_INFO_NO, redInfoNo));
                    inserRed(red, entity1);
                }
            } else {
                res.setCode(response.getCode());
                res.setMsg(response.getMsg());
                return res;
            }
        } catch (Exception e) {
            log.info(CodeConstant.ERROR, "SST接口调用失败或解析失败！:{}", e);
            return Response.error(CodeConstant.ERROR, "SEL_040");
        }
        return Response.success();
    }

    /**
     * 撤销红字申请单
     *
     * @param reqVo
     * @return
     */
    public Response disableTicket(RedTicketOperateReqVO reqVo) {
        StringBuilder tip = new StringBuilder("信息表编号为【");
        List<String> ids = reqVo.getIds();
        Response response = new Response();
        boolean tipFlag = false;
        for (String id : ids) {
            // 查信息表编号
            SellerRedInvoiceEntity entity = this.getById(id);
            String redInfoNo = entity.getRedInfoNo();
            // 根据信息表编号查询发票
            List<SellerInvoiceEntity> invoices = invoicedService.list(new QueryWrapper<SellerInvoiceEntity>().eq("red_info_no", redInfoNo));
            boolean invoiceHasMaked = false;
            if (invoices != null && invoices.size() != 0) {
                // 有发票，判断是否开具
                for (SellerInvoiceEntity invoice : invoices) {
                    String makeInvoiceState = invoice.getMakeInvoiceState();
                    // 如果发票的开票状态是发送开票或已开，则不可撤销红字申请单
                    if ("1".equals(makeInvoiceState)
                            || "2".equals(makeInvoiceState)) {
                        invoiceHasMaked = true;
                        tipFlag = true;
                    }
                }
            }

            // 发票已开或发送中
            if (invoiceHasMaked) {
                tip.append(redInfoNo + ";");
            } else {
                // 把蓝票的状态更新为正常
                List<SellerInvoiceEntity> sieList = invoicedService.list(new QueryWrapper<SellerInvoiceEntity>().eq("invoice_code", entity.getInvoiceCode())
                        .eq("invoice_no", entity.getInvoiceNo()));
                for (SellerInvoiceEntity sie : sieList) {
                    sie.setInvoiceState("0");
                    sie.setUpdateTime(new Date());
                    invoicedService.updateById(sie);
                }
                // 撤销
                entity.setUpdateTime(new Date());
                entity.setTicketState("3");
                this.updateById(entity);
                // 删除发票
                invoicedService.remove(new QueryWrapper<SellerInvoiceEntity>().eq("red_info_no", redInfoNo));
            }
        }
        tip.append("】的红字申请单已经开票（或开票中），不可撤销。");
        if (tipFlag) {
            Response.error("500", tip.toString());
        }
        return response;
    }

    /**
     * 生成红字预制发票
     *
     * @param reqVo
     * @return
     */
    public Response produceRedPrecastInvoices(RedTicketOperateReqVO reqVo) {
        StringBuilder tip = new StringBuilder("信息表编号【");
        boolean flag = true;
        List<String> ids = reqVo.getIds();
        for (String id : ids) {
            SellerRedInvoiceEntity entity = this.getById(id);
            // 根据信息表编号查询发票表是否已有预制了的发票
            SellerInvoiceEntity vat = invoicedService.getOne(new QueryWrapper<SellerInvoiceEntity>().eq("red_info_no", entity.getRedInfoNo()));
            if (vat != null) {
                // 说明已经生成过红字预制发票
                flag = false;
                tip.append(entity.getRedInfoNo() + ";");
            } else {
                List<SellerRedInvoiceDetailEntity> details = sellerRedInvoiceDetailService.list(new QueryWrapper<SellerRedInvoiceDetailEntity>().eq(SellerRedInvoiceDetailConstant.RED_INFO_ID, entity.getUuid()));
                // 根据蓝票的发票代码、号码查询发票
                SellerInvoiceEntity invoice = invoicedService.getOne(new QueryWrapper<SellerInvoiceEntity>().eq("invoice_code", entity.getInvoiceCode())
                        .eq("invoice_no", entity.getInvoiceNo()));
                SellerInvoiceEntity redInvoice = DozerBeanUtil.map(invoice, SellerInvoiceEntity.class);
                String uuid = UUIDUitl.UUID32(false);
                String serialNo = UUIDUitl.UUID32(false);
                // 发票代码、号码、开票日期置空
                redInvoice.setInvoiceCode("");
                redInvoice.setInvoiceNo("");
                redInvoice.setInvoiceDate("");
                redInvoice.setUuid(uuid);
                redInvoice.setSerialNo(serialNo);
                redInvoice.setMakeInvoiceType("1");
                redInvoice.setPurchaserName(entity.getPurchaserName());
                redInvoice.setPurchaserTaxNo(entity.getPurchaserTaxNo());
                redInvoice.setSellerName(entity.getSellerName());
                redInvoice.setSellerTaxNo(entity.getSellerTaxNo());
                redInvoice.setTotalSum(entity.getTotalSum());
                redInvoice.setTotalTax(entity.getTotalTax());
                redInvoice.setTotalSumTax(entity.getTotalSumTax());
                redInvoice.setTaxRate(entity.getTaxRate());
                redInvoice.setRedInfoNo(entity.getRedInfoNo());
                redInvoice.setInvoiceState("0");
                redInvoice.setMakeInvoiceState("0");
                redInvoice.setPrintState("0");
                redInvoice.setDataSource("3");
                redInvoice.setRemark("开具红字增值税专用发票信息表编号" + entity.getRedInfoNo());
                redInvoice.setOldInvoiceNo(entity.getInvoiceNo());
                redInvoice.setOldInvoiceCode(entity.getInvoiceCode());
                List<SellerInvoiceDetailEntity> detailEntities = new ArrayList<>();
                for (SellerRedInvoiceDetailEntity detail : details) {
                    SellerInvoiceDetailEntity detailEntity = DozerBeanUtil.map(detail, SellerInvoiceDetailEntity.class);
                    detailEntity.setSerialNo(serialNo);
                    detailEntity.setUuid(UUIDUitl.UUID32(false));
                    detailEntities.add(detailEntity);
                }
                invoicedService.save(redInvoice);
                invoiceDetailService.saveBatch(detailEntities);
                invoicedService.updateCancelOrderSate(redInvoice, "N");
            }
        }
        tip.append("】的红字申请单已生成红字预制发票，不可重复生成。");
        if (flag) {
            return Response.success();
        } else {
            return Response.error("500", tip.toString());
        }
    }

    public void inserRed(SellerRedInvoiceEntity red, SellerRedInvoiceEntity entity) {
        if (red == null) {
            List<SellerRedInvoiceDetailEntity> details = entity.getDetails();
            // 插入主表
            this.save(entity);
            // 插入明细
            sellerRedInvoiceDetailService.saveBatch(details);
        }
    }

    /**
     * 解析同步红字信息表返回报文
     *
     * @param output
     * @return
     */
    private List<SellerRedInvoiceEntity> parseSyncTicketsResData(Map<String, Object> output) {
        Map RESPONSE_COMMON_HZFPSQBSHXZ = MapUtils.getMap(output, "RESPONSE_COMMON_HZFPSQBSHXZ");
        Map REDINVREQBILL = MapUtils.getMap(RESPONSE_COMMON_HZFPSQBSHXZ, "REDINVREQBILL");
        List<Map<String, Object>> list = (List<Map<String, Object>>) MapUtils.getMap(REDINVREQBILL, "REDINVREQBILLXX");

        List<SellerRedInvoiceEntity> redEntities = new ArrayList<>();
        for (Map<String, Object> map : list) {
            SellerRedInvoiceEntity redEntity = new SellerRedInvoiceEntity();
            String id = UUIDUitl.UUID32(false);
            redEntity.setUuid(id);
            // 信息表编号
            redEntity.setRedInfoNo(MapUtils.getString(map, "RESBILLNO"));
            // 购方名称
            redEntity.setPurchaserName(MapUtils.getString(map, "GFMC"));
            // 购方税号
            redEntity.setPurchaserTaxNo(MapUtils.getString(map, "GFSH"));
            // 销方名称
            redEntity.setSellerName(MapUtils.getString(map, "XFMC"));
            // 销方税号
            redEntity.setSellerTaxNo(MapUtils.getString(map, "XFSH"));
            // 税率
//            redEntity.setTaxRate(MapUtils.getString(map, "TaxRate"));
            // 未税金额
            redEntity.setTotalSum(MapUtils.getString(map, "HJJE"));
            // 税额
            redEntity.setTotalTax(MapUtils.getString(map, "HJSE"));
            double totalSumTax = BigDecimal.valueOf(Double.parseDouble(redEntity.getTotalSum()) + Double.parseDouble(redEntity.getTotalTax()))
                    .setScale(2, BigDecimal.ROUND_HALF_UP)
                    .doubleValue();
            // 含税金额
            redEntity.setTotalSumTax(MapUtils.getString(map, "HJJE"));
            // 申请方税号
            redEntity.setReqTaxNo(MapUtils.getString(map, "GFSH"));
            // 填开日期
            redEntity.setInputDate(MapUtils.getString(map, "DATE"));
            // 申请日期
            redEntity.setReqDate(redEntity.getInputDate());
            // 申请类型：购方申请
            redEntity.setReqType("1");
            // 蓝票代码
            redEntity.setInvoiceCode(MapUtils.getString(map, "YFPDM"));
            // 蓝票号码
            redEntity.setInvoiceNo(MapUtils.getString(map, "YFPHM"));
            // 申请状态：审批通过
            redEntity.setTicketState("0");

            List<SellerRedInvoiceDetailEntity> details = new ArrayList<>();
            Map redInvReqBillMx = MapUtils.getMap(map, "REDINVREQBILLMX");
            List<Map<String, Object>> goodsDetails = (List<Map<String, Object>>) redInvReqBillMx.get("GOODSMX");
            for (Map<String, Object> entity : goodsDetails) {
                SellerRedInvoiceDetailEntity detail = new SellerRedInvoiceDetailEntity();
                detail.setUuid(UUIDUitl.UUID32(false));
                // 主表的主键id
                detail.setRedInfoId(id);
                // 商品名称
                detail.setGoodsName(MapUtils.getString(entity, "XMMC"));
                // 单位
                detail.setUnit(MapUtils.getString(entity, "JLDW"));
                // 单价
                detail.setUnitPrice(MapUtils.getString(entity, "XMDJ"));
                // 税率
                detail.setTaxRate(MapUtils.getString(entity, "SL"));
                // 规格型号
                detail.setGoodType(MapUtils.getString(entity, "GGXH"));
                // 数量
                detail.setGoodNumber(MapUtils.getString(entity, "XMSL"));
                // 未税金额
                detail.setGoodsSum(MapUtils.getString(entity, "XMJE"));
                // 税额
                detail.setGoodTax(MapUtils.getString(entity, "SE"));
                double goodSumTax = BigDecimal.valueOf(Double.parseDouble(detail.getGoodsSum()) + Double.parseDouble(detail.getGoodTax()))
                        .setScale(2, BigDecimal.ROUND_HALF_UP)
                        .doubleValue();
                // 含税金额
                detail.setGoodSumTax(String.valueOf(goodSumTax));
                // 优惠政策标识
                detail.setPreferentialMark(MapUtils.getString(entity, "SFYHZC"));
                // 优惠政策名称
                detail.setPreferentialName(MapUtils.getString(entity, "YHZCNR"));
                // 零税率标识
                detail.setTaxRateMark(MapUtils.getString(entity, "LSLBS"));
                details.add(detail);
            }
            redEntity.setDetails(details);
            redEntities.add(redEntity);
        }
        return redEntities;
    }

}
